//
//  SEGActivityCommentDetailViewModel.m
//  Commom
//
//  Created by 梁学彰 on 2020/6/19.
//

#import "SEGActivityCommentDetailViewModel.h"
 
@implementation SEGActivityCommentDetailViewModel
#pragma mark - life cycle
- (instancetype)initWithQuizId:(NSString *)quizId actId:(NSString *)actId actName:(NSString *)actName {
    if (self = [super init]) {
        _quizID = quizId;
        _actId = actId;
        _actName = actName;
        [self seg_creatActivityCommentDetailCommand];
        [self seg_creatQuizAnswListCommand];
        [self seg_creatPraiseCommand];
        [self seg_creatSaveQuizAnswerCommand];
        //
    }
    return self;
}

#pragma mark - init


#pragma mark - send request
- (void)seg_creatActivityCommentDetailCommand {
    @weakify(self);
    self.activityCommentDetailCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            @strongify(self);
            
            [self.activityManager requestActivityCommentDetailByQuizId:self.quizID Finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
                //添加刷新控件 步骤4：停止动画
                [subscriber sendNext:nil];
                if (isSuccessed) {
                    [subscriber sendCompleted];
                }
                else {
                    [subscriber sendError:[NSError errorWithDomain:NSURLErrorDomain code:100 userInfo:@{NSLocalizedDescriptionKey:errorMessage}]];
                }
            }];
            
            return nil;
        }];
    }];
}

- (void)seg_creatQuizAnswListCommand {
    @weakify(self);
    self.quizAnswListCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(NSNumber *input) {
        @strongify(self);
        if (input.boolValue) {
            self.pageNo = 1;
        }
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            @strongify(self);
            //请求评论回复列表
            __weak typeof (self) weakSelf = self;
            [self.activityManager requestQuizAnswListByQuizId:self.quizID pageNo:self.pageNo Finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
                //添加刷新控件 步骤4：停止动画
                [subscriber sendNext:nil];
                if (!isSuccessed) {
                    [subscriber sendError:[NSError errorWithDomain:NSURLErrorDomain code:100 userInfo:@{NSLocalizedDescriptionKey:errorMessage}]];
                }
                else {
                    weakSelf.pageNo += 1;//数据加载成功后加pageNo
                    [subscriber sendCompleted];
                }
            }];
            
            return nil;
        }];
    }];
}

- (void)seg_creatPraiseCommand {
    @weakify(self);
    self.praiseCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(NSIndexPath *indexPath) {
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            @strongify(self);
            
            if(indexPath.section == 0) {//点赞主贴
                SEGActivityDetailCommentDetailModel *model = self.activityManager.commentDetailModel;
                if ([model.isParise isEqualToString:@"1"]) {
                    //这里取消点赞行为方法
                    [self.activityManager requestCancelPraiseByobjId:model.quizId
                                                             objType:@"2"
                                                            Finished:^(BOOL isSuccessed, id result, NSString* codeType, NSString* errorMessage) {
                        model.isParise = @"0";
                        model.praiseTotal = [NSString stringWithFormat:@"%d",[model.praiseTotal intValue] - 1];
                        [subscriber sendCompleted];
                    }];
                }else{
                    //这里执行点赞行为方法
                    [self.activityManager requestPraiseAttentByObjId:model.quizId
                                                             ObjType:@"2"
                                                     objPicUrlForMsg:model.quizPic
                                                      objTitleForMsg:model.quizContent
                                                      byReviewUserId:model.userId
                                                         parentObjId:model.quizId
                                                       parentObjType:@"6"
                                                       operationType:nil
                                                            Finished:^(BOOL isSuccessed, id result, NSString* codeType, NSString* errorMessage) {
                        model.isParise = @"1";
                        model.praiseTotal = [NSString stringWithFormat:@"%d",[model.praiseTotal intValue] + 1];
                        [subscriber sendCompleted];
                    }];
                }
            }else {//点赞评论的回复
                SEGActivityDetailCommentAnswModel *answModel = self.activityManager.commentAnswerList[indexPath.row];
                if ([answModel.isPraise isEqualToString:@"1"]) {
                    //这里取消点赞行为方法
                    [self.activityManager requestCancelPraiseByobjId:answModel.answId
                                                             objType:@"4"
                                                            Finished:^(BOOL isSuccessed, id result, NSString* codeType, NSString* errorMessage) {
                        answModel.isPraise = @"0";
                        answModel.pariseTotal = [NSString stringWithFormat:@"%d",[answModel.pariseTotal intValue] - 1];
                        [subscriber sendCompleted];
                    }];
                }else{
                    //这里执行点赞行为方法
                    [self.activityManager requestPraiseAttentByObjId:answModel.answId
                                                             ObjType:@"4"
                                                     objPicUrlForMsg:@""
                                                      objTitleForMsg:answModel.answContent
                                                      byReviewUserId:answModel.userId
                                                         parentObjId:answModel.quizId
                                                       parentObjType:@"6"
                                                       operationType:nil
                                                            Finished:^(BOOL isSuccessed, id result, NSString* codeType, NSString* errorMessage) {
                        answModel.isPraise = @"1";
                        answModel.pariseTotal = [NSString stringWithFormat:@"%d",[answModel.pariseTotal intValue] + 1];
                        [subscriber sendCompleted];
                    }];
                }
            }
            return nil;
        }];
    }];
    self.praiseCommand.allowsConcurrentExecution = YES;
}

- (void)seg_creatSaveQuizAnswerCommand {
    @weakify(self);
    self.saveQuizAnswerCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(NSString *content) {
        //去掉前后空格回车
        NSCharacterSet  *set = [NSCharacterSet whitespaceAndNewlineCharacterSet];
        content = [content stringByTrimmingCharactersInSet:set];
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            @strongify(self);
            
            if (content.length == 0) {
                [subscriber sendError:[NSError errorWithDomain:NSURLErrorDomain code:100 userInfo:@{NSLocalizedDescriptionKey:@"请填写内容"}]];
                return nil;
            }else if (content.length > 140){
                [subscriber sendError:[NSError errorWithDomain:NSURLErrorDomain code:100 userInfo:@{NSLocalizedDescriptionKey:@"亲，您输入的内容已超过140字。"}]];
                return nil;
            }
            
            [subscriber sendNext:@(1)];
            if(self.selectedIndexPath.section == 0) {//回复主贴
                [self.activityManager requestSaveQuizAnswerByquizId:self.quizID
                                                        answContent:content
                                                         byReviewId:self.activityManager.commentDetailModel.quizId
                                                       byReviewNick:self.activityManager.commentDetailModel.userName
                                                     byReviewUserId:self.activityManager.commentDetailModel.userId
                                                    objPicUrlForMsg:self.activityManager.commentDetailModel.quizPic
                                                     objTitleForMsg:self.activityManager.commentDetailModel.quizContent
                                                           objeType:self.activityManager.commentDetailModel.objType
                                                        parentObjId:self.activityManager.commentDetailModel.quizId
                                                         paramsType:1002
                                                      parentObjType:6
                                                           Finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
                    
                    [subscriber sendNext:@(0)];
                    
                    if (isSuccessed) {
                    
                        //只有回复主贴加积分
                        [[SEGAnalyseBehaviorManager sharedManager] requestIntegralGrowthWithCode:SEGIntegralCode.reply finished:^(BOOL isSuccessed, id result, NSString* codeType, NSString* errorMessage) {
                        }];
                        [subscriber sendCompleted];
                    }
                    else {
                        [subscriber sendError:[NSError errorWithDomain:NSURLErrorDomain code:codeType.integerValue userInfo:@{NSLocalizedDescriptionKey:errorMessage}]];
                    }
                }];
            }else {//回复评论的回复
                SEGActivityDetailCommentAnswModel *answModel = self.activityManager.commentAnswerList[self.selectedIndexPath.row];
                [self.activityManager requestSaveQuizAnswerByquizId:self.quizID
                                                        answContent:content
                                                         byReviewId:answModel.byReviewId
                                                       byReviewNick:answModel.answNick
                                                     byReviewUserId:answModel.userId
                                                    objPicUrlForMsg:@""
                                                     objTitleForMsg:answModel.answContent
                                                           objeType:self.activityManager.commentDetailModel.objType
                                                        parentObjId:self.activityManager.commentDetailModel.quizId
                                                         paramsType:0
                                                      parentObjType:6
                                                           Finished:^(BOOL isSuccessed, id result, NSString* codeType, NSString* errorMessage) {
                    [subscriber sendNext:@(0)];
                    
                    if (isSuccessed) {
                        [subscriber sendCompleted];
                    }
                    else {
                        [subscriber sendError:[NSError errorWithDomain:NSURLErrorDomain code:codeType.integerValue userInfo:@{NSLocalizedDescriptionKey:errorMessage}]];
                    }
                }];
            }
            
            
            return nil;
        }];
    }];
}

#pragma mark - custom delegate

#pragma mark - system delegate

#pragma mark - event response

#pragma mark - private methods

#pragma mark - setter and getter
- (SEGActivityManager *)activityManager
{
    if (!_activityManager) {
        _activityManager = [[SEGActivityManager alloc] init];
    }
    return _activityManager;
}
- (SEGCommunitySocialManager *)socialManager {
    if (!_socialManager) {
        _socialManager = [[SEGCommunitySocialManager alloc] init];
    }
    return _socialManager;
}

- (NSMutableDictionary *)cellHightDict {
    if (!_cellHightDict) {
        _cellHightDict = [NSMutableDictionary new];
    }
    return _cellHightDict;
}
@end
